package com.xxxx.controller;

import com.xxxx.exception.UserNoLoginException;
import com.xxxx.pojo.ResponseResult;
import com.xxxx.pojo.User;
import com.xxxx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

import static com.xxxx.utils.RandomValidateCode.RANDOMCODEKEY;

/**
 * @Author Lionel
 * @Date 2021/3/7 21:44
 * Created by IntelliJ IDEA 2020.1.
 * Project: house
 * Description：用户操作Controller
 */
@Controller
@RequestMapping("/user")
public class UserController extends BaseController{

    @Autowired
    private UserService userService;

    /**
     * 跳转注册页面
     * @return 注册页面
     */
    @GetMapping("/toRegister")
    public String toRegister() {
        return "register";
    }

    /**
     * 显示登录页面
     * @return 登录页面
     */
    @GetMapping("/toLogin")
    public String toLogin() {
        return "login";
    }

    /**
     * 用户注册功能
     * @param username 用户名
     * @param password 密码
     * @param role 角色
     * @param session session对象
     * @return
     */
    @PostMapping("/register")
    @ResponseBody
    public ResponseResult<Void> userRegister(
            @RequestParam("username") String username,
            @RequestParam("password") String password,
            @RequestParam("role") int role,
            HttpSession session
    ) {
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setRole(role);
        user.setStatus(1);
        userService.insert(user);
        session.setAttribute("username", username);
        return new ResponseResult<Void>();
    }

    /**
     * 用户登录
     * @return
     */
    @PostMapping("/login")
    @ResponseBody
    public ResponseResult<Integer> userLogin(
            @RequestParam("username") String username,
            @RequestParam("password") String password,
            @RequestParam("vertifyCode") String vertifyCode,
            HttpSession session
    ) {
        System.out.println("前台输入验证码:" + vertifyCode);

        ResponseResult<Integer> response = new ResponseResult<>();

        // 从session中获取之前保存的验证码跟前台传来的验证码进行匹配
        Object kaptcha = session.getAttribute(RANDOMCODEKEY);

        if(!vertifyCode.equals(kaptcha)){
            response.setState(502);
        }

        Boolean result = userService.login(username, password);

        if (result) {
            System.out.println("用户存在,登录成功");
            response.setData(userService.getLoginRole(username));
        }

        session.setAttribute("username", username);
        return response;
    }

    /**
     * 退出登录
     * @param session
     * @return
     */
    @GetMapping("/logout")
    public String userLogout (HttpSession session) {
        session.removeAttribute("username");
        session.invalidate();
        return "login";
    }

    /**
     * 登录成功
     * @param session
     * @return
     * @throws UserNoLoginException
     */
    @PostMapping("/loginSuccess")
    @ResponseBody
    public ResponseResult<Void> loginSuccess (
            HttpSession session
    ) throws UserNoLoginException {
        String username = (String) session.getAttribute("username");
        ResponseResult<Void> result = new ResponseResult<>();
        if (StringUtils.isEmpty(username)) {
            throw new UserNoLoginException("用户未登录");
        } else {
            if (session.getAttribute("username").toString() != null) {
                result.setState(200);
            }
        }
        return result;
    }
}
